#!/bin/bash

# Only upload version tags.
if ! [[ "$TRAVIS_TAG" =~ ^v[0-9] ]]; then
  echo Skipping release upload because this build is not for a release tag.
  return 0
fi

if [ -z "$GITHUB_TOKEN" ]; then
  echo "Don't appear to have GitHub token, cannot continue."
  return 0
fi

# Make archives.
echo Archiving releases...
ACME_DIR="$(pwd)"
cd "$GOPATH/releasing/idist"
for x in *; do
  echo "$x"
  cp "$GOPATH/src/github.com/$TRAVIS_REPO_SLUG/.travis/dist-readme.md" "$x"/README.md || \
  cp "$GOPATH/src/github.com/$TRAVIS_REPO_SLUG/README.md" "$x"/ || true
  tar -zcf "../dist/$(basename "$x").tar.gz" "$x"
done

# Must be in the right directory when calling ghr.
cd "$ACME_DIR"

echo Uploading releases...
TRAVIS_REPO_OWNER="$(echo "$TRAVIS_REPO_SLUG" | sed 's#/.*##g')"
travis_retry ghr -u "$TRAVIS_REPO_OWNER" "$TRAVIS_TAG" "$GOPATH/releasing/dist/"

echo Done
